package com.hello.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hello.sky.mapper.DishMapper;
import com.hello.sky.mapper.OrdersMapper;
import com.hello.sky.mapper.SetmealMapper;
import com.hello.sky.mapper.UserMapper;
import com.hello.sky.model.dto.DataOverViewQueryDTO;
import com.hello.sky.model.entity.Orders;
import com.hello.sky.model.vo.BusinessDataVO;
import com.hello.sky.model.vo.DishOverViewVO;
import com.hello.sky.model.vo.OrderOverViewVO;
import com.hello.sky.model.vo.SetmealOverViewVO;
import com.hello.sky.service.WorkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ThreadPoolExecutor threadPool;

    @Override
    public BusinessDataVO businessData(DataOverViewQueryDTO dto) {
        BusinessDataVO vo = new BusinessDataVO();
        LocalDate begin = dto.getBegin() != null ? dto.getBegin() : LocalDate.now();
        LocalDate end = dto.getEnd() != null ? dto.getEnd() : LocalDate.now();

        //营业额
        Double amount = ordersMapper.amountBySingleDate(begin, end); // 修改拼写错误
        if (amount == null) amount = 0.0;
        vo.setTurnover(amount);

        //有效订单数
        Integer valid = ordersMapper.countValidBySingleDate(begin, end, Orders.COMPLETED); // 修改拼写错误
        vo.setValidOrderCount(valid);

        //订单完成率
        Integer count = ordersMapper.countBySingleDate(begin, end); // 修改拼写错误
        vo.setOrderCompletionRate(count.equals(0) ? 0.0 : BigDecimal.valueOf(valid).divide(BigDecimal.valueOf(count), 2, BigDecimal.ROUND_HALF_UP).doubleValue());

        //平均客单价
        vo.setUnitPrice(amount.equals(0.0) && count.equals(0) ? 0.0 : BigDecimal.valueOf(amount).divide(BigDecimal.valueOf(count), 2, BigDecimal.ROUND_HALF_UP).doubleValue());

        //新增用户数
        Integer user = userMapper.countBySingleDate(begin, end); // 修改拼写错误
        vo.setNewUsers(user == null ? 0 : user);
        return vo;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        return setmealMapper.overviewSetmeals();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        return dishMapper.overviewDishes();
    }

    @Override
    public OrderOverViewVO overviewOrders() {

        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        List<Orders> ordersList = ordersMapper.selectList(new LambdaQueryWrapper<Orders>().gt(Orders::getOrderTime, begin).le(Orders::getOrderTime, end));

        //待接单数量
        CompletableFuture<Long> countStatusTwo = CompletableFuture.supplyAsync(() -> ordersList.stream().filter(orders -> orders.getStatus().equals(Orders.TO_BE_CONFIRMED)).count(), threadPool);
        //待派送数量
        CompletableFuture<Long> countStatusThree = CompletableFuture.supplyAsync(() -> ordersList.stream().filter(orders -> orders.getStatus().equals(Orders.CONFIRMED)).count(), threadPool);
        //已完成数量
        CompletableFuture<Long> countStatusFour = CompletableFuture.supplyAsync(() -> ordersList.stream().filter(orders -> orders.getStatus().equals(Orders.COMPLETED)).count(), threadPool);
        //已取消数量
        CompletableFuture<Long> countStatusSix = CompletableFuture.supplyAsync(() -> ordersList.stream().filter(orders -> orders.getStatus().equals(Orders.CANCELLED)).count(), threadPool);

        //全部订单
        int total = ordersList.size();
        try {
            CompletableFuture.allOf(countStatusTwo, countStatusThree, countStatusFour, countStatusSix);
            return new OrderOverViewVO(Integer.valueOf(
                    countStatusTwo.get().toString()),
                    Integer.valueOf(countStatusThree.get().toString()),
                    Integer.valueOf(countStatusFour.get().toString()),
                    Integer.valueOf(countStatusSix.get().toString()),
                    total);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
